﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BusStation
{
    public class TestGenerator
    {
        static Random random;
        const int minIntervals = 50000;
        const int maxIntervals = 50001;
        static int minHours = 0;
        static int maxHours = 24;
        static int minMinutes = 0;
        static int maxMinutes = 59;

        public static void CreateTest(string inputFileName)
        {
            string sequence = CreateSequence();
            PrintInput(@"..\..\Tests\" + inputFileName, sequence);
        }

        public static string CreateSequence()
        {
            random = new Random();
            StringBuilder test = new StringBuilder();

            test.AppendLine(GenerateInterval(random));
            int sequenceLength = random.Next(minIntervals, maxIntervals);

            for (int i = 0; i < sequenceLength - 1; i++)
            {
                test.Append(GenerateInterval(random, random.Next(minHours, maxHours)) + ",");

            }

            return test.ToString();
        }

        private static string GenerateInterval(Random random, int hours)
        {
            StringBuilder interval = new StringBuilder();
            int m = random.Next(minMinutes, maxMinutes);
            interval.Append("[");
            interval.Append(hours.ToString().PadLeft(2, '0') + ":" + m.ToString().PadLeft(2, '0'));
            interval.Append("-");
            m = random.Next(m + 1, maxMinutes);
            interval.Append(hours.ToString().PadLeft(2, '0') + ":" + m.ToString().PadLeft(2, '0'));
            interval.Append("]");
            return interval.ToString();
        }

        private static string GenerateInterval(Random random)
        {
            StringBuilder interval = new StringBuilder();
            int h = random.Next(minHours, maxHours);
            int m = random.Next(minMinutes, maxMinutes);
            interval.Append("[");
            interval.Append(h.ToString().PadLeft(2, '0') + ":" + m.ToString().PadLeft(2, '0'));
            interval.Append("-");
            int hSecond = random.Next(h, maxHours);
            if (h == hSecond)
            {
                m = random.Next(m + 1, maxMinutes);
            }
            else
            {
                m = random.Next(minMinutes, maxMinutes);
            }
            interval.Append(hSecond.ToString().PadLeft(2, '0') + ":" + m.ToString().PadLeft(2, '0'));
            interval.Append("]");
            return interval.ToString();

        }

        private static void PrintInput(string fileName, string sequence)
        {
            StreamWriter writer = new StreamWriter(fileName);

            using (writer)
            {
                writer.WriteLine(sequence);
            }
        }
    }
}
